#include "MY_QT_DEF.h"
void Screen_Win(WId wid,QString str)
{
    QScreen * screen = QGuiApplication::primaryScreen();
    QPixmap p = screen->grabWindow(wid);
    QImage image = p.toImage();
    image.save(str);
}

bool Judg_Dir_Exist(QString fullPath)
{
    QDir dir;
    if(dir.exists(fullPath))
    {
        return true;
    }
    else
    {
        //不存在当前目录，创建，可创建多级目录
        bool ok = dir.mkpath(fullPath);
        return ok;
    }
}

bool Judg_File_Exist(QString fullPath)
{
    QFile file(fullPath);

    if(file.exists())
    {
        return true;
    }
    else
    {
        file.open( QIODevice::ReadWrite | QIODevice::Text );

        //注意关闭文件
        file.close();
    }
    return false;
}

void File_Write_Text(QString fullPath,QString str)
{
    QFile file(fullPath);

    if(file.exists())
    {

    }
    else
    {
        file.open( QIODevice::ReadWrite | QIODevice::Text );

        //注意关闭文件
        file.close();
    }


    file.open( QIODevice::WriteOnly | QIODevice::Text );
    QTextStream stream(&file);
    stream<<str;
    //注意关闭文件
    file.close();
}

void File_Append_Text(QString fullPath,QString str)
{
    QFile file(fullPath);

    if(file.exists())
    {

    }
    else
    {
        file.open( QIODevice::ReadWrite | QIODevice::Text );

        //注意关闭文件
        file.close();
    }


    file.open( QIODevice::Append | QIODevice::Text );
    QTextStream stream(&file);
    stream<<str;
    //注意关闭文件
    file.close();
}

void TextEdit_Append(QTextEdit *text,QString str)
{
    text->moveCursor(QTextCursor::End);
    text->insertPlainText(str);
}

QList<uint8_t> Trans_Hex_Str_to_Buf(QString str)
{
    uint32_t i=0;
    uint32_t str_len = str.length();
    QStringList str_buf;
    QList<uint8_t> buf;

    for(i=0;i<str_len;i++)
    {
        if(str[i]!=" ")
        {
            str_buf.append((QString)str[i]);
        }
    }
    str_len = str_buf.size();
    uint32_t buf_len = str_len/2;

    if (str_len==1)
    {
        buf.append(str_buf[0].toUInt(nullptr,16));
        return buf;
    }

    for(i=0;i<buf_len;i++)
    {
        buf.append(str_buf[i*2].toUInt(nullptr,16)<<4|str_buf[i*2+1].toUInt(nullptr,16));
    }
    return buf;
}

QString Trans_Hex_Buf_to_Str(QList<uint8_t> buf)
{
    uint32_t i=0;
    uint32_t buf_len = buf.size();
    char str[3]={0,0,0};
    QString str_buf="";
    for(i=0;i<buf_len;i++)
    {
        sprintf(str,"%02X",buf[i]);
        str_buf=str_buf+str+" ";
    }

    str_buf = str_buf.left(str_buf.size()-1);
    return str_buf;
}

QString Trans_Hex_Buf_to_Str(uint8_t* buf,uint32_t size)
{
    uint32_t i=0;
    char str[3]={0,0,0};
    QString str_buf="";

    for(i=0;i<size;i++)
    {
        sprintf(str,"%02X",buf[i]);
        str_buf=str_buf+str+" ";
    }

    str_buf = str_buf.left(str_buf.size()-1);
    return str_buf;
}

__attribute__((weak)) void MessageOutput_Callback(QString strMessage)
{
    Q_UNUSED(strMessage);
}

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex MessageOutput_Mutex;
    MessageOutput_Mutex.lock();
    (void)context;
    QByteArray localMsg = msg.toLocal8Bit();

    //信息分类
    QString strMsg("");
    switch(type)
    {
    case QtDebugMsg:
        strMsg = QString("Debug:");
        break;
    case QtInfoMsg:
        strMsg = QString("Info:");
        break;
    case QtWarningMsg:
        strMsg = QString("Warning:");
        break;
    case QtCriticalMsg:
        strMsg = QString("Critical:");
        break;
    case QtFatalMsg:
        strMsg = QString("Fatal:");
        break;

    default:
        break;
    }

    // 设置输出信息格式
    QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    QString strMessage = QString("%1 %2 %3").arg(strDateTime,strMsg,localMsg.constData());

    try
    {
        MessageOutput_Callback(strMessage);
        MessageOutput_Mutex.unlock();
    }
    catch(int i)
    {
        MessageOutput_Mutex.unlock();
    }
}

void Init_MessageOutput(bool EnableNotDisable)
{
    if(EnableNotDisable)
    {
        qInstallMessageHandler(myMessageOutput);
        return;
    }
    qInstallMessageHandler(nullptr);
}

void Init_MY_QChartView_Float_Axis_Struct(MY_QChartView_Float_Axis_Struct *Stu,QChart *chart)
{
    Stu->axis->setLabelFormat(Stu->format);
    Stu->axis->setTitleText(Stu->tittle);
    chart->addAxis(Stu->axis, Stu->alignment);
    Stu->axis->setRange(Stu->min,Stu->max);
}

void Init_MY_QChartView_Float_Struct(MY_QChartView_Float_Struct *Stu)
{
    Stu->chart=new QChart();
    Stu->Series = new QLineSeries();
    Stu->X.axis=new QValueAxis();
    Stu->Y.axis=new QValueAxis();

    Stu->chart->legend()->hide();
    Stu->chart->setTitle(Stu->tittle);
    Stu->chart->addSeries(Stu->Series);

    Init_MY_QChartView_Float_Axis_Struct(&Stu->X,Stu->chart);
    Init_MY_QChartView_Float_Axis_Struct(&Stu->Y,Stu->chart);

    Stu->Series->attachAxis(Stu->X.axis);
    Stu->Series->attachAxis(Stu->Y.axis);

    Stu->chartView->setChart(Stu->chart);
    Stu->chartView->setRenderHint(QPainter::Antialiasing);
}

//添加数据自动移动函数
void Add_Remove_MY_QChartView_Float_Value(MY_QChartView_Float_Struct *Stu,float value)
{
    Stu->data.append(value);
    while (Stu->data.size() > Stu->maxSize)
    {
        Stu->data.removeFirst();
    }

    Stu->Series->clear();

    float xSpace = (Stu->X.max-Stu->X.min) / (Stu->maxSize - 1);

    for (int i = 0; i < Stu->data.size(); ++i)
    {
        Stu->Series->append(xSpace * i, Stu->data.at(i));
    }
}

void Add_MY_QChartView_Float_Value(MY_QChartView_Float_Struct *Stu,float value)
{
    Stu->X.max=Stu->X.max+1;
    Stu->X.axis->setRange(Stu->X.min,Stu->X.max);
    Stu->data.append(value);
    Stu->Series->clear();

    for (int i = 0; i < Stu->data.size(); ++i)
    {
        Stu->Series->append(i, Stu->data.at(i));
    }
}

void Show_MY_QChartView_Float_Struct(MY_QChartView_Float_Struct *Stu)
{
    float delta=(Stu->Y.max-Stu->Y.min)/10.0f;
    Stu->X.axis->setRange(Stu->X.min,Stu->X.max);
    if(Stu->Y.min>=Stu->Y.max)
    {
        Stu->Y.axis->setRange(Stu->Y.min-0.5,Stu->Y.min+0.5);
    }
    else
    {
        Stu->Y.axis->setRange(Stu->Y.min-delta,Stu->Y.max+delta);
    }
    Stu->Series->clear();

    for (int i = 0; i < Stu->data.size(); ++i)
    {
        Stu->Series->append(i, Stu->data.at(i));
    }
}



